package com.example.grape_effect.effet.transition

import android.opengl.Matrix
import com.example.grape_effect.animation.LYAnimation
import com.example.grape_effect.animation.TranslateAnimation
import com.example.grape_effect.gl.common.GLProgram
import com.example.grape_effect.base.GrapeEffect
import com.example.grape_effect.gl.glutils.Matrix2DUtils

open class TranslateTransition(name1: String): GLProgram() {
    companion object {
        var leftToRight = "transition_leftToRight"
        var rightToLeft = "transition_rightToLeft"
        var topToBottom = "transition_topToBottom"
        var bottomToTop = "transition_bottomToTop"
    }
    init {
        name = name1
    }

    val duration = 1600L
    //进入画面的动画
    private lateinit var inAnimation: TranslateAnimation
    var repeatable = true

    //退出画面的动画
    var outAnimation = LYAnimation()
    var outAnimationTranslateMatrix = Matrix2DUtils.getIdentityMatrix()

    // 进入动画和退出的画面的距离 =  outPosition - inPosition
    private var dInOut = floatArrayOf(0f, 0f, 0f)

    init {
        initTransitionWithDirection(name)
        inAnimation.mode = LYAnimation.ANIMATION_MODE_BY_SET_TIME
        inAnimation.duration = duration
        inAnimation.isRepeatable = repeatable
        inAnimation.setInTexture(GrapeEffect.testInTexture2)
        outAnimation.setInTexture(GrapeEffect.testInTexture)
    }

    fun initTransitionWithDirection(name: String) {
        when (name) {
            leftToRight -> {
                dInOut = floatArrayOf(2f, 0f, 0f)
                inAnimation = TranslateAnimation(TranslateAnimation.leftToRight)
            }
            rightToLeft -> {
                dInOut = floatArrayOf(-2f, 0f, 0f)
                inAnimation= TranslateAnimation(TranslateAnimation.rightToLeft)
            }
            topToBottom -> {
                dInOut = floatArrayOf(0f, -2f, 0f)
                inAnimation = TranslateAnimation(TranslateAnimation.topToBottom)
            }
            bottomToTop -> {
                dInOut = floatArrayOf(0f, 2f, 0f)
                inAnimation = TranslateAnimation(TranslateAnimation.bottomToTop)
            }
        }
    }

    override val vertexShader: String? =null

    override val fragmentShader: String? = null
    override fun addParameters() {
    }

    override fun setDefaultParameterValues() {
    }


    override fun draw() {

        if (inAnimation.startTime == LYAnimation.invalidTimeValue) {
            inAnimation.startTime = System.currentTimeMillis()
        }
        val executedTime = System.currentTimeMillis() - inAnimation.startTime
        clearColor()
        inAnimation.updateCurPosition(executedTime)
        inAnimation.draw()
        outAnimationTranslateMatrix = Matrix2DUtils.getIdentityMatrix()
        Matrix2DUtils.reset(outAnimationTranslateMatrix)
        Matrix.translateM(
            outAnimationTranslateMatrix,
            0,
            inAnimation.curPosition[0] + dInOut[0],
            inAnimation.curPosition[1] + dInOut[1],
            inAnimation.curPosition[2] + dInOut[2]
        )
        outAnimation.setMatrix(outAnimationTranslateMatrix)
        outAnimation.draw()
    }


}